package com.agileboot.api.controller.login;

import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.domain.app.user.command.AppLoginDTO;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.app.AppLoginUser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 调度日志操作处理
 *
 * @author ruoyi
 */

@RestController
@RequestMapping("/common")
@AllArgsConstructor
public class LoginController extends BaseController {
    /**
     * 工具类文档地址: https://plus.hutool.cn/apidocs/
     */


    private final ApiUserService apiUserService;


    /**
     * 添加贷款申请
     *
     * @param bean 贷款申请对象
     * @return 生成的JWT令牌
     */
    @PostMapping("/apply/login")
    public ResponseDTO<?> appLogin(@RequestBody AppLoginDTO bean) {

        // 1.通过手机号生成jwt token
        // 2.后续接口都验证token
        String login = apiUserService.loginOrRegister(bean);
        AppLoginUser appLoginUser = AuthenticationUtils.getAppLoginUser();

        AtomicReference<HashMap<String, Object>> map = new AtomicReference<>(new HashMap<>());
        map.get().put("token", login);
        map.get().put("info", appLoginUser);
        return ResponseDTO.ok(map.get());
    }

    /**
     * 发送验证码
     *
     * @param loanCommand
     * @return
     */
    @PostMapping("/send/sms")
    public ResponseDTO<?> sendSms(@RequestBody AppLoginDTO loanCommand) {
        apiUserService.sendSms(loanCommand);
        return ResponseDTO.ok();
    }

    @PostMapping("api/mobile/up/Login/Login")
    public ResponseDTO<?> login() {

        // json转hashMap
        String json = "{\"account\":\"longhaiyang\",\"account_type\":3,\"account_status\":1,\"org_id\":\"1\",\"org_name\":\"中联区卫\",\"property\":1,\"dept_name\":\"门诊外科\",\"dept_code\":\"002\",\"dept_prop\":\"A,B,C,D,E\",\"dept_discipline\":null,\"token\":\"...\",\"token_effective_period\":1723032960,\"...\":\"...\"}";

        ObjectMapper mapper = new ObjectMapper();
            // 将JSON字符串解析为Map<String, Object>
        Map<String, Object> result = null;
        try {
            result = mapper.readValue(json, Map.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }




        return ResponseDTO.ok(result);
    }
}
